//
//  KVComposeViewController.swift
//  KVBestMicroblog
//
//  Created by 李康卫 on 16/7/28.
//  Copyright © 2016年 李康卫. All rights reserved.
//

import UIKit

class KVComposeViewController: UIViewController {
    override func viewDidLoad() {
        self.view.backgroundColor = UIColor.blueColor()
        setupNagationItem()
        setupTextView()

    }
    //设置textView
    private func setupTextView() {
        self.view.addSubview(textView)
        self.textView.showsHorizontalScrollIndicator = true
        if textView.dragging {
            textView.resignFirstResponder()
        }
    }
    //设置导航栏
    private func setupNagationItem() {
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "cancel")
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发布", style: UIBarButtonItemStyle.Plain, target: self, action: "send")
        //设置tiltleView
        self.navigationItem.rightBarButtonItem?.enabled = false
        self.navigationItem.titleView = titleView
    }
    // MARK: - 点击事件
    @objc private func cancel() {
        print("取消")
    }
    @objc private func send() {
        print("发布")
   
    }

    // MARK: - 懒加载
    private lazy var titleView: UILabel = {
        let tiltleView = UILabel()
        let userName: String = (KVUserAccountViewModel.sharedUserAccountViewModel.userAccount?.screen_name)!
        let textContent = "发微博 \n \(userName)"
        tiltleView.text = textContent
        tiltleView.textAlignment = NSTextAlignment.Center
        tiltleView.numberOfLines = 0
        tiltleView.sizeToFit()
        //创建可变文本属性
        let attributedStr = NSMutableAttributedString(string: textContent)
//        let nsUserName: NSString =  userName as NSString
        let range: NSRange = (textContent as NSString).rangeOfString(userName)
        attributedStr.addAttributes([NSFontAttributeName: UIFont.systemFontOfSize(11),NSForegroundColorAttributeName: UIColor.lightGrayColor()], range: range)
        //减属性文本赋值个文本属性
        tiltleView.attributedText = attributedStr
        
//        tiltleView.font = UIFont.systemFontOfSize(14)

        return tiltleView
    }()
    ///加载textView
    private lazy var textView: KVPlaceHolderTextView = {
        let textView = KVPlaceHolderTextView(frame: CGRect(x: 0, y: 0, width: UIScreen.width(), height: UIScreen.height()))
        textView.font = UIFont.systemFontOfSize(15)
        textView.delegate = self
        textView.placeHolderText = "真好玩"
        return textView
    }()
}
extension KVComposeViewController: UITextViewDelegate {
    func textViewDidChange(textView: UITextView) {
        print("textViewDidChange")
        self.navigationItem.rightBarButtonItem?.enabled = textView.hasText()
    }
   
}